import db from "../db";

export default class SqlStatement {
    private sql: string;
    private countSql: string;

    constructor(sql: string) {
        this.sql = sql;
    }

    run(){
        return new Promise((resolve, reject) => {
            console.log("正在执行数据库操作：SQL：" + this.sql);
            db.run(this.sql, (res, err) => {
                if (err) {
                    reject(err)
                }
                return resolve(res)
            });
        });
    }

    get(){
        return new Promise((resolve, reject) => {
            console.log("正在执行数据库操作：SQL：" + this.sql);
            db.get(this.sql, (err, rows) => {
                if (err) {
                    reject(err)
                }
                resolve(rows)
            });
        });
    }

    all(){
        return new Promise((resolve, reject) => {
            console.log("正在执行数据库操作：SQL：" + this.sql);
            db.all(this.sql, (err, rows) => {
                if (err) {
                    reject(err)
                }
                resolve(rows)
            });
        });
    }

    count(){
        return new Promise((resolve, reject) => {
            console.log("正在执行数据库操作：SQL：" + this.countSql);
            db.get(this.countSql, (err, rows) => {
                if (err) {
                    reject(err)
                }
                resolve(rows)
            });
        });
    }

    listExec(sql){
        return new Promise((resolve, reject) => {
            console.log("正在执行数据库操作：SQL：" + sql);
            db.all(sql, (err, rows) => {
                if (err) {
                    reject(err)
                }
                resolve(rows)
            });
        });
    }

    oneExec(sql){
        return new Promise((resolve, reject) => {
            console.log("正在执行数据库操作：SQL：" + sql);
            db.get(sql, (err, rows) => {
                if (err) {
                    reject(err)
                }
                resolve(rows)
            });
        });
    }

    setCountSql(sql: string) {
        this.countSql = sql;
    }
}
